package com.example.wang.myapplication;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;
import com.liqi.nohttputils.interfa.OnIsRequestListener;
import com.liqi.nohttputils.nohttp.RxRequestConfig;
import com.liqi.nohttputils.nohttp.RxRequestOperate;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author jiangyun
 * @version 创建时间：2017/9/7 09:39
 */
public class MyConfigBuilder extends RxRequestConfig.ConfigBuilder {


    public <T> RxRequestOperate buildes(@NonNull Class<String> clazz, final OnMyIsRequestListener1<T> listener) {
        return builder(clazz, new OnIsRequestListener<String>() {
            @Override
            public void onNext(String s) {
                T o = new Gson().fromJson(s, get(listener));
                listener.onResponse(o);
            }

            @Override
            public void onError(Throwable throwable) {
                listener.onErrors(throwable);
            }
        });
    }



    Type mType;

    public Type get(Object o) {
        return getSuperclassTypeParameter(o.getClass());
    }

    static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }
}
